package com.example.demo3network;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.ref.WeakReference;
import java.util.Random;

public class DiglettActivity extends AppCompatActivity implements View.OnClickListener , View.OnTouchListener {

    public static final int CODE = 123;
    public static final int RANDOM_NUMBER = 500;
    private ImageView mImageView;
    private TextView mTextView;
    private Button mButton;
    public int[][] mPosition = new int[][]{
            {342, 180},{432, 880},
            {521, 256},{429, 780},
            {456, 976},{145, 665},
            {123, 678},{564, 567},
    };

    private int mTotalCount;
    private int mSuccessCount;

    public static final int MAX_COUNT = 10;
    private DiglettHandler mHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_diglett);

        mHandler = new DiglettHandler(this, getMainLooper());

        findViews();
        setListener();

    }

    void findViews() {
        mImageView = findViewById(R.id.image_view);
        mTextView = findViewById(R.id.text_view);
        mButton = findViewById(R.id.start_button);
    }

    void setListener() {
//        mImageView.setOnClickListener(this);
        mTextView.setOnClickListener(this);
        mButton.setOnClickListener(this);

        mImageView.setOnTouchListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.start_button:
                start();
                break;
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            view.setVisibility(View.GONE);
            mSuccessCount ++ ;
            mTextView.setText("打到了" + mSuccessCount + "只，共" + MAX_COUNT + "只");
            view.performClick();
            return true;
        }

        return false;
    }

    private void start() {

        mTextView.setText("开始了");
        mButton.setText("游戏中");
        mButton.setEnabled(false);

        next(0);
    }

    private void next(int delayTime) {
        int position = new Random().nextInt(mPosition.length);
        Message message = Message.obtain();
        message.what = CODE;
        message.arg1 = position;

        mHandler.sendMessageDelayed(message, delayTime);
        mTotalCount ++;
    }

    public static class DiglettHandler extends Handler {
        public final WeakReference<DiglettActivity> mWeakReference;
        public DiglettHandler(DiglettActivity activity, Looper looper) {
            super(looper);
            mWeakReference = new WeakReference<>(activity);
        }

        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            DiglettActivity activity = mWeakReference.get();

            switch (msg.what) {
                case CODE:
                    if (activity.mTotalCount > MAX_COUNT) {
                        activity.clear();
                        Toast.makeText(activity, "Game over", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int position = msg.arg1;
                    activity.mImageView.setX(activity.mPosition[position][0]);
                    activity.mImageView.setY(activity.mPosition[position][1]);
                    activity.mImageView.setVisibility(View.VISIBLE);

                    int randomTime = new Random().nextInt(RANDOM_NUMBER) + RANDOM_NUMBER;
                    activity.next(randomTime);

                    break;
            }


        }
    }

    private void clear() {
        mTotalCount = 0;
        mSuccessCount = 0;
        mImageView.setVisibility(View.GONE);
        mButton.setText("点击开始");
        mButton.setEnabled(true);
    }

}
